﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portfolio.IViews.Admin;
using Portfolio.BL.Articles;
using Portfolio.BL.ArticleCategories;
using Portfolio.BL.Interfaces;
using Portfolio.Entities;
using Portfolio.Presenters.Interfaces;

namespace Portfolio.Presenters.Admin
{
    public class AdminEditArticlePresenter : IDefaultPresenter
    {
        private IAdminEditArticleView _view;
        private IArticlesBL _articleBL;
        private ArticleCategoriesBL _categoriesBL;
        private IList<ArtilcesGridV> _articlesList;

        private int _idArticle;
        private int _idArticleCategory;

        public AdminEditArticlePresenter(IAdminEditArticleView view)
        {
            _view = view;
            _articleBL = new ArticlesBL();
            _categoriesBL = new ArticleCategoriesBL();
            _articlesList = new List<ArtilcesGridV>();
            this.Initialize();
        }

        public void Initialize()
        {
            this._view.LoadArticle += new EventHandler<EventArgs>(_viewLoadArticle);
            this._view.LoadCategories += new EventHandler<EventArgs>(_viewLoadCategories);
            this._view.LoadStatus += new EventHandler<EventArgs>(_viewLoadStatus);
            this._view.UpdateArticle += new EventHandler<EventArgs>(_viewUpdateArticle);
            this._view.InsertArticle += new EventHandler<EventArgs>(_viewInsertArticle);
            this._view.InsertExitArticle += new EventHandler<EventArgs>(_viewInsertExitArticle);
        }

        private void _viewLoadArticle(object sender, EventArgs e)
        {
            
            try
            {
                _idArticle = _view.IdArticle;
                if (_idArticle != 0)
                {
                    
                    _articlesList.Add(_articleBL.ReturnArticleById(_idArticle));
                
                    
                
                    _idArticleCategory = _articlesList[0].ArticleCategoryID;
                }
                _view.ArticleDataBind = _articlesList;
            }
            catch (Exception ex)
            {
                _view.PublishException = "Błąd podczas ładowania artykułu! " + ex.Message;
            }
        }

        private void _viewLoadCategories(object sender, EventArgs e)
        {

            try
            {
                //_view.CategoriesDataSource = new IViews.Common.DataSourceContainer("Name", "ArticleCategoryID", _categoriesBL.ReturnCategoriesDDL(null), _idArticleCategory);
            }
            catch (Exception ex)
            {
                _view.PublishException = "Błąd podczas ładowania kategorii! " + ex.Message;
            }
        }

        private void _viewLoadStatus(object sender, EventArgs e)
        {
            int? _status;
            try
            {
                if (_articlesList.Count == 0)
                {
                    _status = null;
                }
                else
                {
                    _status = _articlesList[0].Status;
                }
                
                _view.StatusDataSource = new IViews.Common.DataSourceContainer("Value", "Key", _articleBL.ReturnStatusEnum(), _status);
            }
            catch (Exception ex)
            {
                _view.PublishException = "Błąd podczas ładowania statusu! " + ex.Message; 

            }
        }

        private void _viewUpdateArticle(object sender, EventArgs e)
        {

            try
            {
                _articleBL.UpdateArticle(_view.AtriclesData);
                _view.IsArticleUpdated = true;
            }
            catch (Exception ex)
            {
                //System.Diagnostics.Debug.WriteLine(ex.Message);
                _view.PublishException = "Błąd podczas zmiany artykułu! " + ex.Message; 

            }
        }

        private void _viewInsertExitArticle(object sender, EventArgs e)
        {

            try
            {
                _articleBL.InsertArticle(_view.AtriclesData);
                _view.IsArticleInsert = true;
            }
            catch (Exception ex)
            {
                _view.PublishException = "Błąd podczas dodawania artykułu! " + ex.Message;
            }
        }
        
        private void _viewInsertArticle(object sender, EventArgs e)
        {

            try
            {
                _view.IdArticle = _articleBL.InsertArticle(_view.AtriclesData);
                _view.IsArticleInsert = true;                
            }
            catch (Exception ex)
            {                
                _view.PublishException = "Błąd podczas dodawania artykułu! " + ex.Message;
            }
        }
        
    }
}
